/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2007
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/

namespace epContentBuilder
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using System.Collections;
	using System.ComponentModel;
	using System.Web.SessionState;
	using System.Web.UI;
	using DotNetNuke;
    
	/// <summary>
	///		Summary description for EditTaxonomy.
	/// </summary>
	public abstract class EditTaxonomy : DotNetNuke.PortalModuleControl
	{
		protected int parentNode = -1;
		protected bool selectedFound = false;
		protected TaxonomyController txControl = new TaxonomyController();
		protected int foundedNode = -1;
		protected FunkeLab.MyTreeView TaxonomyTreeView;
		protected System.Web.UI.WebControls.Button cmdUpdate;
		protected System.Web.UI.WebControls.Button cmdDelete;
		protected System.Web.UI.WebControls.Button cmdCancel;
		protected System.Web.UI.HtmlControls.HtmlInputText TaxonomyTitle;
		protected FreeTextBoxControls.FreeTextBox TaxonomyDescription;
		protected System.Web.UI.HtmlControls.HtmlInputHidden TaxonomyHidden;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Build TreeView PS: dbnull is the root virtual element

			if (!IsPostBack) {
				createTree();
			}

		}

		private void createTree () {
			TaxonomyTreeView.Items.Clear();

			FunkeLab.MyTreeViewItem item = new FunkeLab.MyTreeViewItem();
			

			item.Text = "Root";
			item.Selector = FunkeLab.MyTreeViewItem.SelectionMode.RadioButton;
			item.Selected = true;
			item.Value = "-1";
			
			TaxonomyTreeView.Items.Add(this.createTreeNode(-1,item));

			TaxonomyTreeView.OpenAll();

			cmdDelete.Visible = false;
		}

		private FunkeLab.MyTreeViewItem createTreeNode(int node, FunkeLab.MyTreeViewItem localItem){
			TaxonomyController txControl = new TaxonomyController();

			IDataReader reader = (IDataReader)txControl.GetTaxonomySubTree(node);

			while (reader.Read()) {
				// Crate treeview item
				FunkeLab.MyTreeViewItem item = new FunkeLab.MyTreeViewItem();

				item.Text		= reader["Title"].ToString();
				item.Value		= reader["TaxonomyId"].ToString();
				item.OnClick	= "javascript: alert(this)";

				if (Int32.Parse(item.Value) == parentNode) {
					item.Selected	= true;
				} else {
					item.Selected	= false;
				}

				item.Selector	= FunkeLab.MyTreeViewItem.SelectionMode.RadioButton;
				item.Image      = "DeskTopModules/BookShelf/images/node.gif";

				FunkeLab.MyTreeViewItem newNode = createTreeNode(Int32.Parse(item.Value), item);
				  
				localItem.Items.Add(newNode);
			}

			return localItem;
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

			this.cmdUpdate.Click += new System.EventHandler(this.cmdUpdate_Click);
			this.cmdDelete.Click += new System.EventHandler(this.cmdDelete_Click);
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);

			//this.treeView.OnNodeLoadOnDemand += new System.EventHandler(this.treeView_OnNodeLoadOnDemand);
			this.TaxonomyTreeView.OnNodeClick += new FunkeLab.MyTreeView.OnNodeClickEventHandler(this.TaxonomyTreeView_OnNodeClick);
		}
		#endregion
					 
		private void TaxonomyTreeView_OnNodeClick (ref FunkeLab.MyTreeViewItem item) {
			if (item.Value != "") {
				TaxonomyInfo info = txControl.GetItem(Int32.Parse(item.Value));

				TaxonomyHidden.Value		= info.TaxonomyId.ToString();
				TaxonomyTitle.Value			= info.Title;
				TaxonomyDescription.Text	= info.Description;

				parentNode					= info.ParentId;

				createTree();

				cmdDelete.Visible = true;
			}

		}
		private void cmdUpdate_Click (object sender, System.EventArgs e) {
			TaxonomyInfo info = new TaxonomyInfo();

			info.Title = TaxonomyTitle.Value;
			info.Description = TaxonomyDescription.Text;

			// Precisa melhorar

			int tempInt = 1;
			
			FunkeLab.MyTreeViewItem item = TaxonomyTreeView._Items.GetNode(Int32.Parse(Request["_ctl0:_ctl0:_ctl0:"].ToString()),ref tempInt);

			info.ParentId = Int32.Parse(item.Value);

			if (TaxonomyHidden.Value == "") {	
				txControl.Add(info);
			} else {
				info.TaxonomyId = Int32.Parse(TaxonomyHidden.Value);
				txControl.Update(info);
			}
			clearState();
		}

		private void clearState() {
			parentNode = -1;

			createTree();

			cmdDelete.Visible = false;

			TaxonomyTitle.Value = "";
			TaxonomyDescription.Text = "";
			TaxonomyHidden.Value = "";
		}

		private void cmdDelete_Click (object sender, System.EventArgs e) {
			if (TaxonomyHidden.Value != "") {
				txControl.Delete(Int32.Parse(TaxonomyHidden.Value));
				this.clearState();
			}
		}
		private void cmdCancel_Click (object sender, System.EventArgs e) {
			Response.Redirect(get_NavigateURL(""));
		}

	}
}
